define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/appointments/takeAppointmentTemplate.html',
  'text!templates/appointments/emailPopupTemplate.html',
  'fullcalendar',
  'views/appointments/AppointmentPopupView' 
], function($, _, Backbone, appointmentTemplate,emailPopupTemplate,fullCalendar,AppointmentPopupView){

  var TakeAppointmenttView = Backbone.View.extend({
    el: $(".mainpanel"),
        events: { "click .emaillink" :"showEmailPopup"} ,
    render: function(doctorId,hospitalId){
    	self=this;
   	 $.ajax({
	        type: "GET",
	        url: "/getDoctorAppointments?doctorId=" + doctorId+"&hospitalId="+hospitalId,
	        success: function(data){
	        	self.showCalendar(data,doctorId,hospitalId);
	        }
	    });


    },
    showEmailPopup: function(event){
    	 var compiledTemplate = _.template( emailPopupTemplate, null );
	      $('.modal').html(compiledTemplate);
	        $(".modal").lightbox_me({centered: true, onLoad: function() {
				$("#modal_window").find("input:first").focus();
			}
	        });
        	$("#requestAppointment").on('click', function(e){
        		$.ajax({
        			  type: "POST",
        			  url: '/requestAppointment',
        			  data: {
        				  date: $('.fc-col0').text() ,
        				  requesttime:$('#requesttime').val(),
        				  comments:$('#otherdetails').val()
        			  },
        			  success: function(){
        		      		 $.msgBox({
        						    title:"Appment",
        						    content:"We will look at request and revert. Thanks!!",
        						    type:"info"
        						});
		  
        			  },
        			  dataType: 'json'
        			});
        		
        	});


    },
    showCalendar: function(data,doctorId,hospitalId){
 		$("#doctorId").val(doctorId);
		var event =[];
		for(var i in data){
			var appment = data[i];
			if(appment.dateOfAppointment !=null){
				
				var date = new Date(appment.dateOfAppointment);
		 		var d = date.getDate();
		 		var m = date.getMonth();
		 		var y = date.getFullYear();
		 		var endDate = new Date();
		 		var hour=((appment.appointmentTime.substring(0,2)*1) + (appment.appointmentDuration.substring(0,2)*1));
		 		var min=((appment.appointmentTime.substring(3,5)*1)+(appment.appointmentDuration.substring(3,5)*1));
		 		endDate.setHours(hour);
		 		endDate.setMinutes(min);
		 		event[i] = {title: "",start: new Date(y,m,d,appment.appointmentTime.substring(0,2),appment.appointmentTime.substring(3,5)), end: new Date(y,m,d,endDate.getHours(),endDate.getMinutes()),allDay: false,aid: appment.id};		 		
				
			}
		}
		  var compiledTemplate = _.template( appointmentTemplate, null);
	      this.$el.html(compiledTemplate);
	      var dateToShow = this.getRouter.searchView.model.get("datepicker");
	      if(!dateToShow){
	   		$('#calendar').fullCalendar({
	 			theme: true,
	 			aid: 1,
	 			header: {
	 				left: 'prev,next today',
	 				center: 'title',
	 				right: 'month,agendaDay'
	 			},
	 			editable: false,
	 			disableDragging: true,
	 			disableResizing: true,
	 			events: event,
	 			doctorId: doctorId,
	 			appointmentId:0,
	 			hospitalId: hospitalId,
	 			appointmentView: new AppointmentPopupView()
	 		});
	    	  
	      }else{
	   		$('#calendar').fullCalendar({
	 			theme: true,
	 			aid: 1,
	 			header: {
	 				left: 'prev,next today',
	 				center: 'title',
	 				right: 'month,agendaDay'
	 			},
	 			editable: false,
	 			disableResizing: true,
	 			disableDragging: true,
	 			events: event,
	 			doctorId: doctorId,
	 			appointmentId:0,
	 			hospitalId: hospitalId,
	 			appointmentView: new AppointmentPopupView(),
	 			defaultView: 'agendaDay'
	 		});
	   		$('.emailus').css('display','block');
	   		var dates = dateToShow.split('/');
	 		$('#calendar').fullCalendar( 'gotoDate', dates[2],Number(dates[0])-1,dates[1]);
	 		$('#calendar').fullCalendar( 'setSchedule', doctorId,hospitalId);
	      }
  		 $('[class="fc-event-inner fc-event-skin"]').click(function() {
				//alert();
			});


    }
  });

  return TakeAppointmenttView;
});
